<?php
/**
 * controller file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/08/23
 * Time: 15:42
 */

namespace Modules\Admin\Http\Controllers;

use HiCommon\Service\RoleService;
use Illuminate\Support\Facades\Request;

class RoleController extends BaseController
{
    /**
     *  角色 列表
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function index()
    {
        try {
            $res = RoleService::get_list();
            echo_error($res);
            return view('admin::role.index', $res['data']);
        } catch (\Exception $exception) {
            return hello_error('失败-' . $exception->getMessage(), [], $exception->getMessage());
        }

    }

    /**
     * 添加 角色 页面
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function create()
    {
        try {
            $arr_data = [
                'arr_permission' => json_encode(cache_permission_for_admin())
            ];
            return view('admin::role.create', $arr_data);
        } catch (\Exception $exception) {
            return hello_error('失败-' . $exception->getMessage(), [], $exception->getMessage());
        }

    }

    /**
     * 添加 角色 操作
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function store()
    {
        try {
            $arr_input = Request::input();
            $arr_input['model'] = 'admin';
            return RoleService::add_one($arr_input);
        } catch (\Exception $exception) {
            return hello_error('失败-' . $exception->getMessage(), [], $exception->getMessage());
        }

    }

    /**
     * 修改 角色 页面
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit()
    {
        try {
            $n_id = Request::get('id', 0);
            $res = RoleService::get_one((int)$n_id);
            echo_error($res);
            $arr_data['arr_data'] = $res['data'];

            //查询所有权限并标记当前角色选择的权限
            $res = RoleService::get_role_permission((int)$n_id);
            echo_error($res);
            $arr_data['arr_permission'] = json_encode($res['data']);

            return view('admin::role.edit', $arr_data);
        } catch (\Exception $exception) {
            return hello_error('失败-' . $exception->getMessage(), [], $exception->getMessage());
        }

    }

    /**
     * 修改 角色 页面操作
     *
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function update()
    {
        try {
            $arr_input = Request::input();
            //        $arr_input['operator_id'] = (int)admin_user_id();
            return RoleService::edit_one($arr_input);
        } catch (\Exception $exception) {
            return hello_error('失败-' . $exception->getMessage(), [], $exception->getMessage());
        }

    }


    /**
     * 更改 角色 状态
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_state()
    {
        try {
            $arr_input = Request::input();
            return RoleService::change_one_state($arr_input);
        } catch (\Exception $exception) {
            return hello_error('失败-' . $exception->getMessage(), [], $exception->getMessage());
        }

    }

    /**
     * 删除 角色
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete()
    {
        try {
            $n_id = (int)Request::get('id', 0);
            return RoleService::delete_one($n_id);
        } catch (\Exception $exception) {
            return hello_error('失败-' . $exception->getMessage(), [], $exception->getMessage());
        }

    }
}